package com.tengfei.api.index;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tengfei.common.annotation.Log;
import com.tengfei.common.annotation.RepeatSubmit;
import com.tengfei.common.core.controller.BaseController;
import com.tengfei.common.core.domain.PageQuery;
import com.tengfei.common.core.domain.R;
import com.tengfei.common.core.page.TableDataInfo;
import com.tengfei.common.core.validate.EditGroup;
import com.tengfei.common.enums.BusinessType;
import com.tengfei.main.index.domain.bo.BannerBo;
import com.tengfei.main.index.domain.vo.BannerVo;
import com.tengfei.main.index.service.IBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 轮播图Controller
 *
 * @author tengfei
 * @date 2022-09-03
 */
@Validated
@Api(value = "轮播图控制器", tags = {"轮播图管理"})
@RestController
@RequestMapping("/index/banner")
public class BannerController extends BaseController {

    @Resource
    private IBannerService iBannerService;

    /**
     * 查询轮播图列表
     */
    @ApiOperation("查询轮播图列表")
    @SaCheckPermission("index:banner:list")
    @GetMapping("/list")
    public TableDataInfo<BannerVo> list(BannerBo bo, PageQuery pageQuery) {
        return iBannerService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取轮播图详细信息
     */
    @ApiOperation("获取轮播图详细信息")
    @SaCheckPermission("index:banner:query")
    @GetMapping("/{bnId}")
    public R<BannerVo> getInfo(@ApiParam("主键")
                               @NotNull(message = "主键不能为空")
                               @PathVariable("bnId") Long bnId) {
        return R.ok(iBannerService.queryById(bnId));
    }

    /**
     * 上传轮播图
     */
    @ApiOperation("上传轮播图")
    @SaCheckPermission("index:banner:add")
    @Log(title = "上传轮播图", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/upload")
    public R<BannerVo> add(@RequestPart("img") MultipartFile file) {
        return R.ok(iBannerService.uploadBannerImg(file));
    }

    /**
     * 修改轮播图
     */
    @ApiOperation("修改轮播图")
    @SaCheckPermission("index:banner:edit")
    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BannerBo bo) {
        return toAjax(iBannerService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除轮播图
     */
    @ApiOperation("删除轮播图")
    @SaCheckPermission("index:banner:remove")
    @Log(title = "轮播图", businessType = BusinessType.DELETE)
    @DeleteMapping("/")
    public R<Void> remove(@ApiParam("bnId")
                          @NotNull(message = "bnId不能为空")
                          @RequestParam("bnId") Long bnId) {
        return toAjax(iBannerService.deleteWithValidByIds(bnId) ? 1 : 0);
    }
}
